home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / 3D No 'doz II / Source / Sources / main.cp < prev   
Encoding:
Text File  |  1998-06-20  |  4.1 KB  |  212 lines  |  [TEXT/CWIE]

  1. #include <Traps.h>
  2. #include <A4Stuff.h>
  3. #include "Drawing.h"
  4. #include <Gestalt.h>
  5.  
  6. typedef pascal void (*DrawPictPP)(PicHandle, Rect*);
  7. typedef pascal Handle (*GetPictPP)(short);
  8. typedef pascal void (*InitWindPP)(void);
  9.  
  10. GetPictPP        oldGetPicture;
  11. DrawPictPP        oldDrawPicture;
  12. InitWindPP        oldInitWindows;
  13. short            oldBitDepth;
  14. Boolean            bypass;
  15. Boolean            isRunning;
  16.  
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20.  
  21. pascal Handle MyGetPicture(short);
  22. pascal void MyDrawPicture(PicHandle, Rect*);
  23. pascal void MyInitWindows(void);
  24. static void Cleanup(void);
  25.  
  26. #ifdef __cplusplus
  27. }
  28. #endif
  29.  
  30. Str31                extFileName;
  31. short                extVRefNum;
  32. long                extDirID;
  33. Handle                myHandle;
  34. UniversalProcPtr    myGetPictureJMPAddr, myDrawPictureJMPAddr, myInitWindowsJMPAddr;
  35.  
  36. pascal void main(Handle me)
  37. {
  38.     long        oldA4;
  39.     
  40.     oldA4 = SetCurrentA4();
  41.     
  42.     myHandle = me;
  43.     
  44.     bypass = false;
  45.     isRunning = false;
  46.     
  47.     {
  48.         FCBPBRec    pb;
  49.         
  50.         pb.ioFCBIndx = 0;
  51.         pb.ioVRefNum = 0;
  52.         pb.ioRefNum = CurResFile();
  53.         pb.ioNamePtr = extFileName;
  54.         PBGetFCBInfoSync(&pb);
  55.         
  56.         extVRefNum = pb.ioFCBVRefNum;
  57.         extDirID = pb.ioFCBParID;
  58.     }
  59.     
  60.     myGetPictureJMPAddr = (UniversalProcPtr) NewPtrSys(6*3);            // room for 3 JMP $xxxxxxxx's
  61.     
  62.     if (myGetPictureJMPAddr != nil)
  63.     {
  64.         void*    p = (void*) myGetPictureJMPAddr;
  65.         
  66.         *((short*) p)++ = 0x4EF9;        // JMP
  67.         *((long*) p)++ = (long) MyGetPicture;
  68.         myDrawPictureJMPAddr = (UniversalProcPtr) p;
  69.         *((short*) p)++ = 0x4EF9;        // JMP
  70.         *((long*) p)++ = (long) MyDrawPicture;
  71.         myInitWindowsJMPAddr = (UniversalProcPtr) p;
  72.         *((short*) p)++ = 0x4EF9;        // JMP
  73.         *((long*) p)++ = (long) MyInitWindows;
  74.         
  75.         oldGetPicture = (GetPictPP) GetToolTrapAddress(_GetPicture);
  76.         SetToolTrapAddress(myGetPictureJMPAddr, _GetPicture);
  77.     }
  78.     
  79.     SetA4(oldA4);
  80. }
  81.  
  82. pascal Handle MyGetPicture(short id)
  83. {
  84.     GetPictPP    gp;
  85.     long        oldA4;
  86.     
  87.     oldA4 = SetCurrentA4();
  88.     
  89.     gp = oldGetPicture;
  90.     
  91.     if (!bypass && !isRunning)
  92.     {
  93.         short    cResFile = CurResFile();
  94.         short    myFile;
  95.         Boolean    oldBypass = bypass;
  96.         
  97.         bypass = true;
  98.         
  99.         StopDraw();
  100.         
  101.         isRunning = false;
  102.         
  103.         DoneDraw();
  104.         
  105.         myFile = HOpenResFile(extVRefNum, extDirID, extFileName, fsCurPerm);
  106.         UseResFile(myFile);
  107.         if (InitDraw(id))
  108.             bypass = oldBypass = true;
  109.         CloseResFile(myFile);
  110.         UseResFile(cResFile);
  111.         
  112.         bypass = oldBypass;
  113.         
  114.         if (!bypass)
  115.         {
  116.             UniversalProcPtr    temp;
  117.             
  118.             temp = GetToolTrapAddress(_DrawPicture);
  119.             if (temp != myDrawPictureJMPAddr)
  120.             {
  121.                 oldDrawPicture = (DrawPictPP) temp;
  122.                 SetToolTrapAddress(myDrawPictureJMPAddr, _DrawPicture);
  123.             }
  124.             
  125.             temp = GetToolTrapAddress(_InitWindows);
  126.             if (temp != myInitWindowsJMPAddr)
  127.             {
  128.                 oldInitWindows = (InitWindPP) temp;
  129.                 SetToolTrapAddress(myInitWindowsJMPAddr, _InitWindows);
  130.             }
  131.         }
  132.     }
  133.     
  134.     SetA4(oldA4);
  135.     
  136.     return (*gp)(id);
  137. }
  138.  
  139. pascal void MyDrawPicture(PicHandle pic, Rect* r)
  140. {
  141.     DrawPictPP    dp;
  142.     long        oldA4;
  143.     
  144.     oldA4 = SetCurrentA4();
  145.     dp = (DrawPictPP) oldDrawPicture;
  146.     SetA4(oldA4);
  147.  
  148.     (*dp)(pic, r);
  149.     
  150.     oldA4 = SetCurrentA4();
  151.     if (!bypass && !isRunning)
  152.     {
  153.         // Verify that we haven't already launched the Finder
  154.         if ((0 < LMGetCurApName()[0]) && (LMGetCurApName()[0] < sizeof(Str31)))
  155.             Cleanup();            // If an application is now running, clean up!
  156.     }
  157.     
  158.     if (!bypass && !isRunning)
  159.     {
  160.         GDHandle    mainDev;
  161.         Boolean        oldBypass = bypass;
  162.         
  163.         bypass = true;
  164.         
  165.         mainDev = GetMainDevice();
  166.         
  167.         oldBitDepth = (**(**mainDev).gdPMap).pixelSize;
  168.         
  169.         StartDraw(r, (**mainDev).gdPMap);
  170.         
  171.         isRunning = true;
  172.         
  173.         bypass = oldBypass;
  174.     }
  175.     SetA4(oldA4);
  176. }
  177.  
  178. pascal void MyInitWindows(void)
  179. {
  180.     InitWindPP    iw;
  181.     long        oldA4;
  182.     
  183.     oldA4 = SetCurrentA4();
  184.     iw = (InitWindPP) oldInitWindows;
  185.     
  186.     if (!bypass)
  187.         Cleanup();
  188.     
  189.     SetA4(oldA4);
  190.     
  191.     (*iw)();
  192. }
  193.  
  194. static void Cleanup(void)
  195. {
  196.     StopDraw();
  197.     
  198.     isRunning = false;
  199.     
  200.     if ((0 < LMGetCurApName()[0]) && (LMGetCurApName()[0] < sizeof(Str31)))
  201.     {
  202.         bypass = true;
  203.         
  204.         DoneDraw();
  205.         
  206.         // Now, put the old addresses into the JMP addresses, so that we are fully bypassed
  207.         *(long*) (sizeof(short) + (Ptr) myGetPictureJMPAddr) = (long) oldGetPicture;
  208.         *(long*) (sizeof(short) + (Ptr) myDrawPictureJMPAddr) = (long) oldDrawPicture;
  209.         *(long*) (sizeof(short) + (Ptr) myInitWindowsJMPAddr) = (long) oldInitWindows;
  210.     }
  211. }
  212.